/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Handlers;

import apiAccess.APIAccess;
import apiAccess.APIEXception;
import apiAccess.SubCategory;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.xml.sax.SAXException;

/**
 *
 * @author Kevin
 */
public class SubCategoryHandler {
    List<SubCategory> subCategoryList=new LinkedList<SubCategory>();
    int from;
    int to;
    int cant;
    int page;

    public SubCategoryHandler(int cant, int category_id, int languageId) throws SAXException, IOException, APIEXception{
        this.cant=cant;
        this.from=0-cant;
        this.to=0;
        page=0;
        subCategoryList.add(new SubCategory(-1, category_id,"all", "All"));
        subCategoryList.addAll(APIAccess.getSubCategoryList(category_id, languageId));
    }

   public List<SubCategory> next(){
       if(from+cant>=subCategoryList.size()){
           return null;
       }
       from+=cant;
       page++;
       if(to+cant>=subCategoryList.size()){
            to=subCategoryList.size();
       }else{
           to+=cant;
       }
       List<SubCategory> ret= subCategoryList.subList(from, to);
       return ret;
   }

   public List<SubCategory> back(){
       if(from==0){
           return null;
       }
       to-=to-from;
       from-=cant;
       page--;
       List<SubCategory> ret= subCategoryList.subList(from, to);
       return ret;
   }

   public boolean activoNext(){
       if(from+cant>=subCategoryList.size()){
           return false;
       }
       return true;

   }

    public boolean activoBack(){
       if(from==0){
           return false;
       }
       return true;

   }

    public int cantSubCategories() {
        if(haySubCat())
            return subCategoryList.size();
        return 0;
    }

    public boolean haySubCat(){
        return subCategoryList!=null;
    }

    public int pages() {
        int aux=subCategoryList.size()%this.cant;
        int resp=(int)(subCategoryList.size()/this.cant);
        if(aux!=0){
            resp++;
        }
        return resp;
    }

    public String pagesFoot(){
        return page+"/"+pages();
    }

}
